﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Controls;

#if !SILVERLIGHT
using UIT.WPF.Converters;

namespace UIT.WPF.Renderers
#else
using UIT.Silverlight.Converters;

namespace UIT.Silverlight.Renderers
#endif
{
    class DrawableControlRenderer :
#if !SILVERLIGHT
        WpfRenderer<Controls.IDrawableControl, FrameworkElement>
#else
 SlRenderer<Controls.IDrawableControl, FrameworkElement>
#endif
    {
        public DrawableControlRenderer(FrameworkElement element)
        {
            Result = element;
        }

        public override void Render(Controls.IControl control)
        {
            if (control.IsDrawable)
                Render(control as Controls.IDrawableControl);
        }

        public override void Render(Controls.IDrawableControl control)
        {
            if (Result == null)
                return;
            Result.SetBinding(FrameworkElement.HeightProperty, new Binding("Style.Height"));
            Result.SetBinding(FrameworkElement.WidthProperty, new Binding("Style.Width"));
#if !SILVERLIGHT
            Result.SetBinding(FrameworkElement.MarginProperty, new Binding("Style.Margin") { Converter = new UIT.WPF.Converters.ThicknessConverter() });
            Result.SetBinding(Control.BackgroundProperty, new Binding("Style.Background")
                {
                    Converter = new BrushConverter(GetInStyle<System.Windows.Media.Brush>(Control.BackgroundProperty))
                });
#else
            Result.SetBinding(FrameworkElement.MarginProperty, new Binding("Style.Margin") { Converter = new UIT.Silverlight.Converters.ThicknessConverter() });
			if (Result is Control)
				Result.SetBinding(Control.BackgroundProperty, new Binding("Style.Background")
				{
					Converter = new BrushConverter(GetInStyle<System.Windows.Media.Brush>(Control.BackgroundProperty))
				});
#endif
        }

        private T GetInStyle<T>(System.Windows.DependencyProperty dependencyProperty)
            where T : class
        {
            return GetInStyle(dependencyProperty) as T;
        }

        private object GetInStyle(System.Windows.DependencyProperty dependencyProperty)
        {
            System.Windows.Style style = Result.Style;
#if !SILVERLIGHT
            if (style == null)
                style = (System.Windows.Style)Control.StyleProperty.GetMetadata(Result).DefaultValue;
            if (style == null)
                style = (System.Windows.Style)Application.Current.TryFindResource(Result.GetType());
#else
            if (style == null)
                style = UIT.Sl.Utils.Extensions.FindResource<System.Windows.Style>(Result.GetType());
#endif
            if (style == null)
                return null;
            return GetInStyle(style, dependencyProperty);
        }

        private object GetInStyle(System.Windows.Style style, System.Windows.DependencyProperty dependencyProperty)
        {
            if (style == null)
                return null;
            System.Windows.Setter s = style.Setters.OfType<System.Windows.Setter>().FirstOrDefault(setter => setter.Property == dependencyProperty);
            if (s == null)
                return GetInStyle(style.BasedOn, dependencyProperty);
            return s.Value;
        }
    }
}
